package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IFlowContractService;
import com.mdd.admin.validate.FlowContractCreateValidate;
import com.mdd.admin.validate.FlowContractUpdateValidate;
import com.mdd.admin.validate.FlowContractSearchValidate;
import com.mdd.admin.vo.FlowContractListedVo;
import com.mdd.admin.vo.FlowContractDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.FlowContract;
import com.mdd.common.mapper.FlowContractMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 新建合同流程记录实现类
 * @author LikeAdmin
 */
@Service
public class FlowContractServiceImpl implements IFlowContractService {
        
    @Resource
    FlowContractMapper flowContractMapper;

    /**
     * 新建合同流程记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<FlowContractListedVo>
     */
    @Override
    public PageResult<FlowContractListedVo> list(PageValidate pageValidate, FlowContractSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<FlowContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        flowContractMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:handlingIdea@handling_idea:str",
            "=:remarks:str",
        });

        IPage<FlowContract> iPage = flowContractMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<FlowContractListedVo> list = new LinkedList<>();
        for(FlowContract item : iPage.getRecords()) {
            FlowContractListedVo vo = new FlowContractListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 新建合同流程记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return FlowContract
     */
    @Override
    public FlowContractDetailVo detail(Integer id) {
        FlowContract model = flowContractMapper.selectOne(
                new QueryWrapper<FlowContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        FlowContractDetailVo vo = new FlowContractDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 新建合同流程记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(FlowContractCreateValidate createValidate) {
        FlowContract model = new FlowContract();
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setRemarks(createValidate.getRemarks());
        flowContractMapper.insert(model);
    }

    /**
     * 新建合同流程记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(FlowContractUpdateValidate updateValidate) {
        FlowContract model = flowContractMapper.selectOne(
                new QueryWrapper<FlowContract>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        model.setId(updateValidate.getId());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        model.setUpdateUserId(updateValidate.getUpdateUserId());
        model.setUpdateUserName(updateValidate.getUpdateUserName());
        model.setWorkflowStage(updateValidate.getWorkflowStage());
        model.setAcceptingOfficer(updateValidate.getAcceptingOfficer());
        model.setHandlingTime(updateValidate.getHandlingTime());
        model.setHandlingResult(updateValidate.getHandlingResult());
        model.setHandlingIdea(updateValidate.getHandlingIdea());
        model.setRemarks(updateValidate.getRemarks());
        flowContractMapper.updateById(model);
    }

    /**
     * 新建合同流程记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        FlowContract model = flowContractMapper.selectOne(
                new QueryWrapper<FlowContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        flowContractMapper.updateById(model);
    }

}
